package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.MenschBean;

/**
 * Servlet implementation class MenschServlet
 */
@WebServlet("/MenschServlet")
public class MenschServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MenschServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=iso-8859-1");
		
		HttpSession session = request.getSession(true);

        if (session.getAttribute("gameBean") == null) {
           MenschBean bean = new MenschBean();
           session.setAttribute("gameBean", bean);
        }
        
        if (request.getParameter("action") != null) {
			MenschBean bean = (MenschBean) session.getAttribute("gameBean");

			if (request.getParameter("action").equals("roll")) {
				bean.startTurn();
			}

			session.setAttribute("gameBean", bean);

		}

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/table.jsp");
        dispatcher.forward(request, response);
	}
        
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=iso-8859-1");

        HttpSession session = request.getSession(true);

        MenschBean bean = new MenschBean();
        session.setAttribute("gameBean", bean);

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/table.jsp");
        dispatcher.forward(request, response);
	}
}
